package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.Product;
import com.team.six.bluecatshop.domain.SeachSkuResponse;
import com.team.six.bluecatshop.domain.Sku;
import com.team.six.bluecatshop.service.ProductService;
import com.team.six.bluecatshop.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/adminSku")
public class AdminSkuController {

    @Autowired
    SkuService skuService;
    @Autowired
    ProductService productService;
    @Autowired
    FileController fileController;
    /**
     * 跳转至Sku展示页面
     */
    @RequestMapping("/skuList")
    public String productGetList(Model model){

        return "administration/sku_list";
    }
    /**
     * 处理Sku展示请求
     */
    @GetMapping("/api/skuList")
    @ResponseBody
    public SeachSkuResponse seachSkuResponse(Integer pageNum, Integer pageSize, String keyword){
        return skuService.findSkuAll(pageNum,pageSize,keyword);
    }

    /**
     * 跳转至新增Sku请求页面并展示Sku类型
     */
    @GetMapping("/addSkuShow")
    public String addSkuShow(Model model){

        List<Product> products = productService.findAll();

        model.addAttribute("products",products);
        return "administration/sku-add";
    }

    /**
     * 新增Sku请求
     */
    @PostMapping("/addSku")
    public String addSku(Sku sku,@RequestParam(value = "file") MultipartFile file){

        fileController.upload(file);
        String fname = file.getOriginalFilename(); //文件名称
        sku.setProductId(sku.getProductId());

        //System.out.println(sku.getProductId());
        sku.setImgSrc("/"+fname);

            skuService.add(sku);


        return "redirect:/adminSku/skuList";
    }

    @PostMapping("/editSku")
    public String editSku(Sku sku){

        skuService.updateByPrimaryKey(sku);

        return "redirect:/adminSku/skuList";
    }

    /**
     * 处理删除请求
     */
    @GetMapping("/deleteSku")
    public String deleteSku(String skuId){
        skuService.remove(skuId);
        return "redirect:/adminSku/skuList";
    }

    /**
     * 跳转至修改Sku页面并展示Sku类型
     */
    @GetMapping("/editSkuShow")
    public String editSkuShow(Sku skuEdit,Model model){
        Product product = productService.findById(skuEdit.getProductId());

        model.addAttribute("skuEdit",skuEdit);

        model.addAttribute("product",product);
        return "administration/sku-edit";
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    @ResponseBody
    public String batchDelete(String skus){
        String[] split = skus.split(",");
        for (int i=0;i<split.length;i++){
            //System.out.println(split[i]);
            skuService.remove(split[i]);
        }
        //System.out.println(skus);
        return "删除成功";
    }
}
